package ui.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.peng.auctionsystem.MainActivity;
import com.peng.auctionsystem.R;

import Widget.CustomToolBar;
import base.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import entity.LoginEntity;
import manager.ApiRequestManager;
import manager.RxSubscriber;
import utils.GlobalDataUtils;
import utils.ToastUtil;

/**
 * 登录页
 */
public class LoginActivity extends BaseActivity {


    @Bind(R.id.login_et_amount)
    AutoCompleteTextView loginEtAmount;
    @Bind(R.id.login_et_password)
    EditText loginEtPassword;
    @Bind(R.id.login_btn_login)
    Button loginBtnLogin;
    @Bind(R.id.login_btn_regist)
    Button loginBtnRegist;
    @Bind(R.id.email_login_form)
    LinearLayout emailLoginForm;
    @Bind(R.id.login_form)
    ScrollView loginForm;
    @Bind(R.id.custom_toolbar)
    CustomToolBar customToolbar;
    @Bind(R.id.login_forget_psw)
    TextView loginForgetPsw;

    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        customToolbar.findTextView(R.id.left_tv).setVisibility(View.GONE);
        customToolbar.findImageView(R.id.left_iv).setVisibility(View.GONE);
        customToolbar.findTextView(R.id.right_tv_main).setVisibility(View.GONE);

        if (customToolbar != null) {
            customToolbar.findTextView(R.id.center_tv).setText("登录");
            customToolbar.findTextView(R.id.right_tv_sub).setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.login_btn_login, R.id.login_btn_regist, R.id.login_forget_psw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_btn_login:// 登录

                String account = loginEtAmount.getText().toString();
                String pwd = loginEtPassword.getText().toString();

                if (account == null || account.equals("")) {
                    ToastUtil.showShort("请输入账号");
                    return;
                }
                if (pwd == null || pwd.equals("")) {
                    ToastUtil.showShort("请输入密码");
                    return;
                }
                mRxManager.add(ApiRequestManager.getInstance().login(account, pwd)
                        .subscribe(new RxSubscriber<LoginEntity>(this, "正在登陆...", true, false) {
                            @Override
                            protected void _onNext(LoginEntity baseResult) {

                                SharedPreferences sPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sPreferences.edit();
                                editor.putInt("userId", baseResult.getData().getId());
                                //切记最后要使用commit方法将数据写入文件
                                editor.commit();

                                ToastUtil.showShort("登陆成功");
                                GlobalDataUtils.saveCurUser(baseResult);
                                startActivity(MainActivity.class);
                                finish();
                            }

                            @Override
                            protected void _onError(String message) {
                                ToastUtil.showShort("登陆失败");
                            }
                        }));

                break;
            case R.id.login_btn_regist:// 注册
                startActivity(RegisterActivity.class);
                break;
            case R.id.login_forget_psw:// 忘记密码
                startActivity(ForgetPswActivity.class);
                break;
        }
    }
}

